﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TemplateWizard;
using EnvDTE;
using System.Windows;

namespace WPLDB.TemplateWizard
{
    public class WPLDBWizard : IWizard
    {

        #region Unused Interface Methods

        public void ProjectFinishedGenerating(Project project)
        {
        }

        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
        }

        public void RunFinished()
        {
        }

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
        }

        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
        #endregion

        #region "Interface Implementation"

        public void BeforeOpeningFile(ProjectItem projectItem)
        {
            using (var bootstrapper = new WPLDB.Package.UI.Bootstrapper())
            {
                //Run the Wizard Application.
                bootstrapper.ProjectItem = projectItem;
                bootstrapper.Run();
                projectItem = bootstrapper.GetCurrentItem();


                //Delete the file when cancelling the operation.
                if (bootstrapper.IsCancel == true
                    && null != projectItem)
                    projectItem.Delete();

                //Open the created file
                if (null != projectItem)
                {
                    var projectItemWindow = projectItem.Open(EnvDTE.Constants.vsViewKindCode);
                    projectItemWindow.Visible = true;
                    projectItemWindow.SetFocus();
                }
            }
        }

        #endregion
    }
}
